const messageModel = require('../models/messages.model')
const logger = require('../utils/logger')
class MessageService {
  messages = messageModel
  findAll (query = {}) {
    return this.messages.find({ deleted: false, ...query }).populate('ref')
  }
  findOne (id) {
    return this.messages.find({ deleted: false, _id: id })
  }
  updateOne (id, data) {
    return this.messages.findOneAndUpdate({ _id: id }, data)
  }
  deleteOne (id) {
    return this.messages.findOneAndUpdate({ _id: id }, { deleted: true })
  }
  async addOne (doc) {
    // TODO check doc
    logger.info('message created ', doc)
    return await this.messages.create(doc)
  }
}

module.exports = MessageService